package com.secxiot.consumer.config;

import com.secxiot.common.constant.ServerStatusEnum;
import com.secxiot.common.entity.ResultVo;
import com.secxiot.common.exception.BaseException;
import com.secxiot.common.exception.BaseRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice(annotations = {RestController.class})
public class GlobalExceptionHandler {

    @ExceptionHandler({BaseRuntimeException.class, BaseException.class, Exception.class})
    public ResultVo handleBussinessException(HttpServletRequest request,Exception ex) {
        if (log.isDebugEnabled()) {
            log.error(ex.getMessage(), ex);
        }
        if (ex instanceof BaseException || ex instanceof BaseRuntimeException) {
            return new ResultVo<String>(ex);
        }  else {
            return new ResultVo(ServerStatusEnum.SYSTEM_EXCEPTION);
        }
    }

}
